/*
 * Digital Audio Mobile - A multi track studio for Windows Mobile
 * Copyright (C) 2008 David Olsson
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "Control.h"
#include "Dialog.h"

Control::Control(Dialog *dialog, unsigned int x, unsigned int y, unsigned int width, unsigned int height) throw(): m_parent(dialog), m_hWnd(0) {
    WNDCLASS wc;
    if(!GetClassInfo(m_parent->hInstance(), CLASSNAME, &wc)) {
        ZeroMemory(&wc, sizeof(WNDCLASS));
        wc.lpfnWndProc = wndProc;
        wc.hInstance = m_parent->hInstance();
        wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
        wc.lpszClassName = CLASSNAME;
        RegisterClass(&wc);
    }

    m_hWnd = CreateWindow(CLASSNAME, 0, WS_CHILD | WS_VISIBLE, Dialog::widthToPixels(x), Dialog::heightToPixels(y), Dialog::widthToPixels(width), Dialog::heightToPixels(height), m_parent->hWnd(), 0, m_parent->hInstance(), this);
}

Control::~Control() {
    DestroyWindow(m_hWnd);
}

Dialog *Control::parent() throw() {
    return(m_parent);
}

unsigned int Control::width() const throw() {
    RECT rect;
    GetClientRect(m_hWnd, &rect);
    LONG dlgBase = LOWORD(GetDialogBaseUnits());
    return(MulDiv(rect.right - rect.left, 4, dlgBase));
}

unsigned int Control::height() const throw() {
    RECT rect;
    GetClientRect(m_hWnd, &rect);
    LONG dlgBase = HIWORD(GetDialogBaseUnits());
    return(MulDiv(rect.bottom - rect.top, 8, dlgBase));
}

HWND Control::hWnd() throw() {
    return(m_hWnd);
}

LRESULT CALLBACK Control::wndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    if(uMsg == WM_CREATE) {
        CREATESTRUCT *cs = reinterpret_cast<CREATESTRUCT *>(lParam);
        SetWindowLong(hWnd, GWL_USERDATA, LONG(cs->lpCreateParams));
        return(0);
    }
    else {
        Control *ctrl = reinterpret_cast<Control *>(GetWindowLong(hWnd, GWL_USERDATA));
        if(ctrl != 0) {
            switch(uMsg) {
                case WM_PAINT:{
                    PAINTSTRUCT ps;
                    HDC hDc = BeginPaint(hWnd, &ps);
                    ctrl->paint(hDc);
                    EndPaint(hWnd, &ps);
                }
                case WM_COMMAND:
                    if(lParam != 0) {
                        ctrl->controlNotification(HWND(lParam), HIWORD(wParam));
                    }
                    return(0);
                case WM_LBUTTONDOWN:
                    ctrl->click(Dialog::widthToDialogUnits(LOWORD(lParam)), Dialog::heightToDialogUnits(HIWORD(lParam)));
                    return(0);
                default:
                    return(DefWindowProc(hWnd, uMsg, wParam, lParam));
            }
        }
        else {
            return(DefWindowProc(hWnd, uMsg, wParam, lParam));
        }
    }
}

const TCHAR * const Control::CLASSNAME = TEXT("DamControlClass");